package com.xuxu.blog.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuxu.blog.entity.FileEntity;
import com.xuxu.blog.entity.UserEntity;
import com.xuxu.blog.service.DownLoadService;
import com.xuxu.blog.service.impl.UserServiceImpl;
import com.xuxu.common.utils.R;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @className: DownLoadController
 * @author: Maxuxu
 * @date: 2022/8/28 15:14
 * @description: 下载专用类
 */
@RestController
@RequestMapping("/blog/download")
@Api(value = "DownLoadController", tags = "文件下载")
@Slf4j
public class DownLoadController {
    @Resource
    private DownLoadService downLoadService;
    @Resource
    private UserServiceImpl userService;

    /**
     * 下载资源后扣除积分,本质上是更新用户信息。所有也可以直接调用usercontroller中的更新方法
     * @return
     */
    @PutMapping("/payPoints")
    @ApiOperation(value = "下载后扣除用户积分")
    public R payPoints(@RequestBody UserEntity user) {
        log.error("要跟新的用户信息是：{}",user);
        boolean b = userService.updateById(user);
        if (!b) {
            return R.error("下载失败");
        }
        return R.ok("下载成功");
    }
    /**
     * 下载时查询对应的id
     * @param id
     * @return
     */
    @GetMapping("/findPoints/{id}")
    public R isEnableDownload(@PathVariable("id") int id) {
        FileEntity fileEntity = downLoadService.FindFilesById(id);
        return R.ok().put("data", fileEntity);
    }

    @ApiOperation(value = "分页查询可下载的文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "文件名",paramType = "query",required = false)
    })
    @GetMapping("/page")
    public R findPage(@RequestParam Integer index,
                      @RequestParam Integer size,
                       String name,
                       String type) {
        Page<FileEntity> page = downLoadService.FindByPage(index, size, name, type);
        return R.ok().put("data", page);
    }
}
